package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.constant.MqConstant;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utlis.OrderHolder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @Author www
 * @Description OrderService实现层
 * @Date 2023/4/11 11:28
 * @Version 1.0
 */
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    /**
     * 注入feign
     */
    @Resource
    private ItemClient itemClient;
    /**
     * 注入feign
     */
    @Resource
    private UserClient userClient;

    @Resource
    private OrderDetailService detailService;

    @Resource
    private OrderLogisticsService logisticsService;


    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 创建订单业务
     *
     * @param params 前端数据
     * @return 订单id
     */
    @Transactional
    @Override
    public Long createOrder(RequestParams params) {
        Order order = new Order();
        // 1.查询商品
        Item item = itemClient.queryItemById(params.getItemId());
        // 2.基于商品价格、购买数量计算商品总价：totalFee
        long totalFee = item.getPrice() * params.getNum();
        order.setTotalFee(totalFee);
        order.setPaymentType(params.getPaymentType());
        order.setUserId(OrderHolder.getUser());
        order.setStatus(1);
        // 3.将Order写入数据库tb_order表中
        this.save(order);

        // 4.将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail detail = new OrderDetail();
        detail.setName(item.getName());
        detail.setSpec(item.getSpec());
        detail.setPrice(item.getPrice());
        detail.setNum(params.getNum());
        detail.setItemId(item.getId());
        detail.setImage(item.getImage());
        detail.setOrderId(order.getId());
        detailService.save(detail);


        Long addressId = params.getAddressId();
        // 5.根据addressId查询user-service服务，获取地址信息
        Address address = userClient.findAddressById(addressId);
        // 6.将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address, orderLogistics);
        orderLogistics.setOrderId(order.getId());
        logisticsService.save(orderLogistics);

        //11-在item-service 提供减库存接口，并写入FeignClient
        //12-调用item-service的减库存接口
        try {
            itemClient.updateStock(params.getItemId(), params.getNum());
        } catch (Exception e) {
            throw new RuntimeException("库存不足！");
        }

        //提交订单后 发送队列信息
        rabbitTemplate.convertAndSend(MqConstant.ORDER_EXCHANGE,MqConstant.ORDER_KEY,order.getId());
        System.out.println("信息发送成功");
        return order.getId();
    }



}
